package com.example.ceramics.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.ceramics.R;
import com.example.ceramics.database.dao.IUserDao;
import com.example.ceramics.database.dao.UserDaoImpl;
import com.example.ceramics.entity.PwdSwitch;
import com.example.ceramics.util.StringUtils;

public class LoginActivity extends BaseActivity {

    private EditText etAccount;
    private CheckBox cbRememberPwd;
    private Button btnLogin;

    private PwdSwitch pwd1;

    String spFileName = "sp_login";
    String accountKey = "login_account";
    String pwdKey = "login_pwd";
    String rememberPwdKey = "login_remember_pwd";

    private TextView tvRegister;

    private IUserDao userDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.et_account);
        cbRememberPwd = findViewById(R.id.cb_remember_pwd);
        btnLogin = findViewById(R.id.btn_login);
        pwd1 = new PwdSwitch(findViewById(R.id.et_pwd),findViewById(R.id.iv_pwd_switch));

        tvRegister = findViewById(R.id.tv_register);
    }

    @Override
    protected void initData() {
        init();

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
                String pwd =pwd1.etPwd.getText().toString().trim();
                login(account,pwd);
            }
        });

        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(RegisterActivity.class);
            }
        });
    }

    //初始化
    private void init() {
        SharedPreferences spFile = getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        String account = spFile.getString(accountKey, null);
        String pwd = spFile.getString(pwdKey, null);
        Boolean rememberPwd = spFile.getBoolean(rememberPwdKey, false);

        if (!StringUtils.isEmpty(account)) {
            etAccount.setText(account);
        }
        if (!StringUtils.isEmpty(pwd)) {
            pwd1.etPwd.setText(pwd);
        }

        cbRememberPwd.setChecked(rememberPwd);
    }

    //登录
    private void login(String account, String pwd) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号!");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            showToast("请输入密码!");
            return;
        }

        //记住密码复选框
        SharedPreferences spFile = getSharedPreferences(spFileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = spFile.edit();

        if (cbRememberPwd.isChecked()) {
            editor.putString(accountKey, account);
            editor.putString(pwdKey, pwd);
            editor.putBoolean(rememberPwdKey, true);
            editor.apply();
        } else {
//            editor.remove(accountKey);
            editor.putString(accountKey, account);
            editor.remove(pwdKey);
            editor.remove(rememberPwdKey);
            editor.apply();
        }

        userDao = new UserDaoImpl(this);
        if(userDao.login(account,pwd)){
            //没接口直接登录
            saveStringToSp("account", account);
//        navigateTo(HomeActivity.class, "tab",0,Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            navigateTo(HomeActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            showToast("登录成功");
        }else {
            showToast("账号或者密码错误，请重新输入！");
        }


//        //没接口直接登录
//        saveStringToSp("account", account);
////        navigateTo(HomeActivity.class, "tab",0,Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//        navigateTo(HomeActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//        showToast("登录成功");
    }
}